<?php 
class ShopCart{
	protected $db;
	protected $tab;
	public function __construct($db,$tab='goods_shopcart'){
		$this->db =$db;
		$this->tab =$tab;
		// parent::__construct($db,$tab,$size,$nums);		
	}
		//根据cid获取指定数量（默认全部）的商品
	public function checkUserInfo(){
		$res=array('msg'=>"用户还未登录",'code'=>100,'data'=>'');
		if (isset($_SESSION['mobile'])&&isset($_SESSION['shopCart'])) {
			$res=array('msg'=>"用户已登录",'code'=>101,'data'=>'');
		}
		return $res;
	}

	// 添加商品到数据库
	public function addGoodsToShopCart(){
		$res=array('msg'=>"商品加入购物车失败！", 'code'=>102, 'data'=>'');
		$goodid=$_GET['id'];
		$name=$_GET['name'];
		$price=$_GET['price'];
		$num=$_GET['num'];
		$uid=$_SESSION['uid'];
		$time=time();

		// 先查询是否存在此商品，若存在则更新，否则新添加
		$sql="select goodid,num,price from {$this->tab} where goodid={$goodid} and uid='{$uid}' and status='1'";
		$row=$this->db->selectRow($sql);
		if ($row) {
			$newNum=$num+$row['num'];
			$sql="update {$this->tab} set num='{$newNum}' where goodid={$goodid} and uid='{$uid}' and status='1'";
		} else {
			$sql="insert into {$this->tab}(uid,goodid,goodname,price,num,time) values('{$uid}','{$goodid}','{$name}','{$price}','{$num}','{$time}')";
		}

		if ($this->db->otherData($sql)) {
			$res=array('msg'=>"商品加入购物车成功！",'code'=>103, 'data'=>$sql);
		}

		return $res;
	}

	public function initShopCart() {
		$uid=$_SESSION['uid'];
		$res=array('msg'=>"取商品失败！", 'code'=>104, 'data'=>$uid);

		$sql="select car.id, car.goodid, car.goodname, car.price, car.num, g.img, g.pcs ,g.nums from {$this->tab} as car, goods_list as g where car.status='1' and car.goodid=g.id and car.uid={$uid}";
		$rows=$this->db->selectRows($sql);
		if($rows){
			$res=array('msg'=>"取商品成功！", 'code'=>105, 'data'=>$rows);
		}
		return $res;
	}
	public function changeNum(){
		$res=array('msg'=>"更新商品数量失败！", 'code'=>106, 'data'=>'');
		$goodid=$_GET['goodid'];
		$num=$_GET['num'];
		$uid=$_SESSION['uid'];
		$sql="update {$this->tab} set num='{$num}' where goodid='{$goodid}' and uid='{$uid}' and status='1' ";
		if($this->db->otherData($sql)){
			$res=array('msg'=>"更新商品数量成功！", 'code'=>107, 'data'=>$sql);
		}
		return $res;
	}
	public function delGoods(){
		$res=array('msg'=>"删除商品失败！", 'code'=>110, 'data'=>'');
		$goodid=$_GET['goodid'];
		$uid=$_SESSION['uid'];
		$sql="delete from {$this->tab} where goodid='{$goodid}' and uid='{$uid}' and status='1' ";
		if($this->db->otherData($sql)){
			$res=array('msg'=>"删除商品成功！", 'code'=>111, 'data'=>'');
		}
		return $res;
	}
	public function pushOrder(){
		$res=array('msg'=>"提交订单失败！", 'code'=>108, 'data'=>'');
		$orderid=uniqid().rand(10000,99999);
		$uid=$_SESSION['uid'];
		$time=time();
		$goodsid=$_POST;
		$n=0;
		$money=0;
		foreach ($goodsid as $goodid) {
			$sql_sel="select price,num from {$this->tab} where goodid='{$goodid}' and uid='{$uid}' and status='1' ";
			$row=$this->db->selectRow($sql_sel);
			if($row){
				$money+=floatval($row['price'])*intval($row['num']);				
			}
			$sql_up="update {$this->tab} set status='2', orderid='{$orderid}', time='{$time}' where goodid='{$goodid}' and uid='{$uid}' and status='1' ";
			if($this->db->otherData($sql_up)>0){
				$n++;
			}
		}
		$sql_in="insert into goods_order(uid,orderid,money,order_status,pay_status,time) values('{$uid}','{$orderid}','{$money}','2','2','{$time}')";
		if($this->db->otherData($sql_in)>0){
			$n++;
		}
		if($n>0){
			$res=array('msg'=>"提交订单成功！", 'code'=>109, 'data'=>'');
		}
		return $res;
	}

}
 ?>
